/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool fun(TreeNode* root, int x, int sum)
    {
        if (root == nullptr)return 0;
        sum += root->val;
        if (sum == x) {
            if (root->left == nullptr && root->right == nullptr)return 1;
            return fun(root->left, x, sum) || fun(root->right, x, sum);
        }
        else {
            return fun(root->left, x, sum) || fun(root->right, x, sum);
        }
    }
    bool hasPathSum(TreeNode* root, int targetSum) {
        if (fun(root, targetSum, 0)) {
            return 1;
        }
        return 0;
    }
};